<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="container">
          <el-form size="small" label-width="100px">
            <span class="seachTiele">{{ $t("creationtime") }}：</span>
            <el-radio-group
              v-model="tableFrom.date"
              type="button"
              class="mr20"
              size="small"
              clearable
              @change="selectChange(tableFrom.date)"
            >
              <el-radio-button
                v-for="(item, i) in fromList.fromTxt"
                :key="i"
                :label="item.val"
                >{{ item.text }}</el-radio-button
              >
            </el-radio-group>
            <el-date-picker
              v-model="timeVal"
              value-format="yyyy/MM/dd"
              format="yyyy/MM/dd"
              size="small"
              type="daterange"
              placement="bottom-end"
              :placeholder="$t('customtime')"
              style="width: 250px"
              clearable
              @change="onchangeTime"
            />
            <div class="mt20">
              <span class="seachTiele">分账时间：</span>
              <el-radio-group
                v-model="tableFrom.profit_date"
                type="button"
                class="mr20"
                size="small"
                clearable
                @change="selectChange2(tableFrom.profit_date)"
              >
                <el-radio-button
                  v-for="(item, i) in fromList.fromTxt"
                  :key="i"
                  :label="item.val"
                  >{{ item.text }}</el-radio-button
                >
              </el-radio-group>
              <el-date-picker
                v-model="timeVal2"
                value-format="yyyy/MM/dd"
                format="yyyy/MM/dd"
                size="small"
                type="daterange"
                placement="bottom-end"
                :placeholder="$t('customtime')"
                style="width: 250px"
                clearable
                @change="onchangeTime2"
              />
            </div>
            <div class="mt20">
              <span class="seachTiele">{{ $t("state") }}：</span>
              <el-select
                v-model="tableFrom.status"
                :placeholder="$t('pleaseselect')"
                class="filter-item selWidth mr20"
                clearable
                @change="getList(1)"
              >
                <el-option
                  v-for="item in applyStatus"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
              <span class="seachTiele">分账账单类型：</span>
              <el-select
                v-model="tableFrom.type"
                :placeholder="$t('pleaseselect')"
                class="filter-item selWidth mr20"
                clearable
                @change="getList(1)"
              >
                <el-option :label="$t('orderpayment')" value="order" />
                <el-option :label="$t('finalpayment')" value="presell" />
              </el-select>
              <el-button
                size="small"
                type="primary"
                icon="el-icon-search"
                @click="getList(1)"
                >{{ $t("search") }}</el-button
              >
              <el-button
                size="small"
                type="primary"
                icon="el-icon-top"
                @click="exportRecord"
                >{{ $t("listexport") }}</el-button
              >
              <el-button
                size="small"
                type="primary"
                @click="getExportFileList"
                >{{ $t("exportrecords") }}</el-button
              >
            </div>
          </el-form>
        </div>
      </div>
      <el-table
        v-loading="listLoading"
        :data="tableData.data"
        style="width: 100%"
        size="mini"
      >
        <el-table-column
          prop="profitsharing_id"
          :label="$t('dimensionid')"
          min-width="50"
        />
        <el-table-column
          prop="order.order_sn"
          :label="$t('orderno')"
          min-width="60"
        />

        <el-table-column :label="$t('dimensionamount')" min-width="100">
          <template slot-scope="scope">
            <div>
              {{ $t("orderamount") }}： {{ scope.row.profitsharing_price }}
            </div>
            <div
              v-if="scope.row.profitsharing_refund > 0"
              style="color: #82e493"
            >
              {{ $t("refundamount:") }} {{ scope.row.profitsharing_refund }}
            </div>
            <div>分账给商户金额： {{ scope.row.profitsharing_mer_price }}</div>
          </template>
        </el-table-column>
        <el-table-column :label="$t('billingtype')" min-width="80">
          <template slot-scope="scope">
            <span>{{
              scope.row.type == "order"
                ? $t("orderpayment")
                : $t("finalpayment")
            }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('state')" min-width="100">
          <template slot-scope="scope">
            <div v-if="scope.row.status == 0">
              {{ $t("undistributedaccount") }}
            </div>
            <div v-else-if="scope.row.status == 1">
              {{ $t("allocated") }}<br />分账时间：
              {{ scope.row.profitsharing_time }}
            </div>
            <div v-else-if="scope.row.status == -1">{{ $t("refunded") }}</div>
            <div v-else-if="scope.row.status == -2">
              {{ $t("accountopeningfailed") }}<br />
              <span style="color: red; font-size: 12px">
                {{ $t("failurereason") }}： {{ scope.row.error_msg }}</span
              >
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="create_time"
          :label="$t('creationtime')"
          min-width="100"
        />
      </el-table>
      <div class="block">
        <el-pagination
          :page-sizes="[20, 40, 60, 80]"
          :page-size="tableFrom.limit"
          :current-page="tableFrom.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
    <!--导出订单列表-->
    <file-list ref="exportList" />
  </div>
</template>

<script>
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
import { applymentLstApi, ledgerManageExportApi } from "@/api/system";
import fileList from "@/components/exportFile/fileList";
export default {
  components: { fileList },
  data() {
    return {
      tableData: {
        data: [],
        total: 0,
      },
      listLoading: true,
      tableFrom: {
        type: "",
        keyword: "",
        status: "",
        date: "",
        profit_date: "",
        page: 1,
        limit: 20,
      },
      timeVal: [],
      timeVal2: [],
      fromList: {
        title: this.$t("selectionperiod"),
        custom: true,
        fromTxt: [
          { text: this.$t("all"), val: "" },
          { text: this.$t("today"), val: "today" },
          { text: this.$t("yesterday"), val: "yesterday" },
          { text: this.$t("last7days"), val: "lately7" },
          { text: this.$t("last30days"), val: "lately30" },
          { text: this.$t("thismonth"), val: "month" },
          { text: this.$t("thisyear"), val: "year" },
        ],
      },
      selectionList: [],
      ids: "",
      LogLoading: false,
      applyStatus: [
        { value: 0, label: this.$t("tobedivided") },
        { value: 1, label: this.$t("allocated") },
        { value: -1, label: this.$t("refunded") },
        { value: -2, label: this.$t("accountopeningfailed") },
      ],
      orderDatalist: null,
    };
  },
  mounted() {
    this.getList(1);
  },
  methods: {
    // 选择时间
    selectChange(tab) {
      this.tableFrom.date = tab;
      this.timeVal = [];
      this.getList(1);
    },
    selectChange2(tab) {
      this.tableFrom.profit_date = tab;
      this.timeVal2 = [];
      this.getList(1);
    },
    // 具体日期
    onchangeTime(e) {
      this.timeVal = e;
      this.tableFrom.date = e ? this.timeVal.join("-") : "";
      this.getList(1);
    },
    onchangeTime2(e) {
      this.timeVal2 = e;
      this.tableFrom.profit_date = e ? this.timeVal2.join("-") : "";
      this.getList(1);
    },
    // 导出
    exportRecord() {
      ledgerManageExportApi(this.tableFrom)
        .then((res) => {
          const h = this.$createElement;
          this.$msgbox({
            title: this.$t("tips"),
            message: h("p", null, [
              h("span", null, this.$t("please71")),
              h("span", { style: "color: teal" }, this.$t("exportrecords")),
              h("span", null, this.$t("see")),
            ]),
            confirmButtonText: this.$t("igotit"),
          }).then((action) => {});
        })
        .catch((res) => {
          this.$message.error(res.message);
        });
    },
    // 导出列表
    getExportFileList() {
      this.$refs.exportList.exportFileList();
    },
    // 列表
    getList(num) {
      this.listLoading = true;
      this.tableFrom.page = num || this.tableFrom.page;
      applymentLstApi(this.tableFrom)
        .then((res) => {
          this.tableData.data = res.data.list;
          this.tableData.total = res.data.count;
          this.listLoading = false;
        })
        .catch((res) => {
          this.$message.error(res.message);
          this.listLoading = false;
        });
    },
    pageChange(page) {
      this.tableFrom.page = page;
      this.getList("");
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val;
      this.getList("");
    },
  },
};
</script>

<style lang="scss" scoped>
.selWidth {
  width: 300px;
}
table .el-image {
  display: inline-block;
}
.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
  font-size: 12px;
}
.el-icon-arrow-down {
  font-size: 12px;
}
.tabBox_tit {
  width: 60%;
  font-size: 12px !important;
  margin: 0 2px 0 10px;
  letter-spacing: 1px;
  padding: 5px 0;
  box-sizing: border-box;
}
.mt20 {
  margin-top: 20px;
}
.demo-image__preview {
  position: relative;
}
.maxw180 {
  display: inline-block;
  max-width: 180px;
}
</style>
